//
//  XMGTopicCell.m
//  BuDeJie
//
//  Created by 陈恒均 on 2018/1/31.
//  Copyright © 2018年 小码哥. All rights reserved.
//

#import "XMGTopicCell.h"
#import "XMGTopics.h"
#import <UIImageView+WebCache.h>
#import "XMGTopicPictureView.h"
#import "XMGTopicVideoView.h"
#import "XMGTopicVoiceView.h"

@interface XMGTopicCell()
@property (weak, nonatomic) IBOutlet UIImageView *profileImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *passtimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *text_label;
@property (weak, nonatomic) IBOutlet UIButton *dingButton;
@property (weak, nonatomic) IBOutlet UIButton *caiButton;
@property (weak, nonatomic) IBOutlet UIButton *repostButton;
@property (weak, nonatomic) IBOutlet UIButton *commentButton;
@property (weak, nonatomic) IBOutlet UIView *topCmtView;
@property (weak, nonatomic) IBOutlet UILabel *topCmtLabel;

//中间控件

@property (nonatomic, weak) XMGTopicVideoView *videoView;
@property (nonatomic, weak) XMGTopicVoiceView *voiceView;
@property (nonatomic, weak) XMGTopicPictureView *pictureView;

@end

@implementation XMGTopicCell

#pragma mark - 懒加载
-(XMGTopicVideoView *)videoView
{
    if (_videoView == nil) {
        _videoView = [XMGTopicVideoView xmg_viewFromXib];
        [self.contentView addSubview:_videoView];
    }
    return _videoView;
}
- (XMGTopicPictureView *)pictureView
{
    if (!_pictureView) {
        XMGTopicPictureView *pictureView = [XMGTopicPictureView xmg_viewFromXib];
        [self.contentView addSubview:pictureView];
        _pictureView = pictureView;
    }
    return _pictureView;
}

- (XMGTopicVoiceView *)voiceView
{
    if (!_voiceView) {
        XMGTopicVoiceView *voiceView = [XMGTopicVoiceView xmg_viewFromXib];
        [self.contentView addSubview:voiceView];
        _voiceView = voiceView;
    }
    return _voiceView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    UIImage *image = [UIImage imageNamed:@"mainCellBackground"];
    UIImage *newImage = [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height *0.5];
    self.backgroundView = [[UIImageView alloc] initWithImage:newImage];
}

-(void)setTopic:(XMGTopics *)topic
{
    _topic = topic;
    // 顶部控件的数据
    
    UIImage *placeholder = [UIImage xmg_circleImageNamed:@"defaultUserIcon"];
    [self.profileImageView  sd_setImageWithURL:[NSURL URLWithString:topic.profile_image] placeholderImage:placeholder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (!image) {
            return ;
        }
        self.profileImageView.image = [image xmg_circleImage];
    }];
    self.nameLabel.text = topic.name;
    self.passtimeLabel.text = topic.passtime;
    self.text_label.text = topic.text;
    
    [self setupButtonTitle:self.dingButton number:topic.ding placeholder:@"顶"];
    [self setupButtonTitle:self.caiButton number:topic.cai placeholder:@"踩"];
    [self setupButtonTitle:self.repostButton number:topic.repost placeholder:@"分享"];
    [self setupButtonTitle:self.commentButton number:topic.comment placeholder:@"评论"];
    
    if (topic.top_cmt.count) {
        self.topCmtView.hidden = NO;
        NSDictionary *cmt = topic.top_cmt.firstObject;
        NSString *content = cmt[@"content"];
        if (content.length == 0) {
            content = @"语音评论";
        }
        NSString *username = cmt[@"user"][@"username"];
        self.topCmtLabel.text = [NSString stringWithFormat:@"%@ : %@", username, content];
    }else//没有最热评论
    {
        self.topCmtView.hidden = YES;
    }
    
    if (topic.type == XMGTopicTypePicture) {
        self.pictureView.hidden = NO;
        self.pictureView.topic = topic;
        self.videoView.hidden = YES;
        self.voiceView.hidden = YES;
    }else if (topic.type == XMGTopicTypeVoice)
    {
        self.pictureView.hidden = YES;
        
        self.videoView.hidden = YES;
        self.voiceView.topic = topic;
        self.voiceView.hidden = NO;
    }else if (topic.type == XMGTopicTypeVideo)
    {
        self.pictureView.hidden = YES;
        self.videoView.hidden = NO;
        self.videoView.topic = topic;
        self.voiceView.hidden = YES;
    }else
    {
        self.pictureView.hidden = YES;
        self.videoView.hidden = YES;
        self.voiceView.hidden = YES;
    }

}

-(void)layoutSubviews
{
    [super layoutSubviews];
    if (self.topic.type == XMGTopicTypePicture) { // 图片
        self.pictureView.frame = self.topic.middleFrame;
    } else if (self.topic.type == XMGTopicTypeVoice) { // 声音
        self.voiceView.frame = self.topic.middleFrame;
    } else if (self.topic.type == XMGTopicTypeVideo) { // 视频
        self.videoView.frame = self.topic.middleFrame;
    }
}

- (void)setupButtonTitle:(UIButton *)button number:(NSInteger)number placeholder:(NSString *)placeholder
{
    if (number >= 10000) {
        [button setTitle:[NSString stringWithFormat:@"%.1f万", number / 10000.0] forState:UIControlStateNormal];
    } else if (number > 0) {
        [button setTitle:[NSString stringWithFormat:@"%zd", number] forState:UIControlStateNormal];
    } else {
        [button setTitle:placeholder forState:UIControlStateNormal];
    }
}

-(void)setFrame:(CGRect)frame
{
//    frame.origin.x += 10;
//    frame.size.width -= 20;
    frame.size.height -= 10;
    [super setFrame:frame];
}

@end
